<?php 
class FinanceUtils {
  public static function get_data4many($date, $invoices, $finance_date) {
    $obj = new stdClass();
    $obj->date = $date;
    $obj->open_balance = 0.00; 
    $obj->tuition_ob = 0.00; 
    $obj->living_ob = 0.00; 
    $obj->insurance_ob = 0.00; 
    $obj->tuition_fee = 0.00; 
    $obj->living_fee = 0.00;
    $obj->insurance_fee = 0.00; 
    $obj->tuition_cb = 0.00; 
    $obj->living_cb = 0.00; 
    $obj->insurance_cb = 0.00; 
    $obj->close_balance = 0.00; 
    
    foreach ($invoices as $invoice) {
      $t_table = FinanceUtils::get_t_table($invoice, $finance_date);
      $l_table = FinanceUtils::get_l_table($invoice, $finance_date);
      $i_table = FinanceUtils::get_i_table($invoice, $finance_date);
      
      $tmp = FinanceUtils::get_data4single($date, $t_table, $l_table, $i_table, $invoice);
      $obj->open_balance += $tmp->open_balance;
      $obj->tuition_ob += $tmp->tuition_ob;
      $obj->living_ob += $tmp->living_ob;
      $obj->insurance_ob += $tmp->insurance_ob;
      $obj->tuition_fee += $tmp->tuition_fee;
      $obj->living_fee += $tmp->living_fee;
      $obj->insurance_fee += $tmp->insurance_fee;
      $obj->tuition_cb += $tmp->tuition_cb; 
      $obj->living_cb += $tmp->living_cb;
      $obj->insurance_cb += $tmp->insurance_cb;
      $obj->close_balance += $tmp->close_balance;
    }
    
    return $obj;
  }
  
  public static function get_data4single($date, $t_table, $l_table, $i_table, $invoice) {
    $obj = new stdClass();
    $obj->date = $date;
    $obj->open_balance = 0.00; 
    $obj->tuition_ob = 0.00; 
    $obj->living_ob = 0.00;
    $obj->insurance_ob = 0.00; 
    $obj->tuition_fee = 0.00; 
    $obj->living_fee = 0.00;
    $obj->insurance_fee = 0.00; 
    $obj->tuition_cb = 0.00; 
    $obj->living_cb = 0.00; 
    $obj->close_balance = 0.00; 
    
    $t_row = FinanceUtils::__get_table_row($date, $t_table);
    $l_row = FinanceUtils::__get_table_row($date, $l_table);
    $i_row = FinanceUtils::__get_table_row($date, $i_table);

    $obj->open_balance = $t_row->ob + $l_row->ob + $i_row->ob; 
    $obj->tuition_ob = $t_row->ob; 
    $obj->living_ob = $l_row->ob;
    $obj->insurance_ob = $i_row->ob;
    $obj->tuition_fee = $t_row->fee; 
    $obj->living_fee = $l_row->fee; 
    $obj->insurance_fee = $i_row->fee; 
    $obj->tuition_cb = $t_row->cb;
    $obj->living_cb = $l_row->cb; 
    $obj->insurance_cb = $i_row->cb; 
    $obj->close_balance = $t_row->cb + $l_row->cb + $i_row->cb;
    
    return $obj;
  }
  
  public static function __get_table_row($date, $table) {
    foreach ($table as $row) {
      if ($row->date == $date) {
        return $row;
      }
    }
    
    $obj = new stdClass();
    $obj->date = $date;
    $obj->ob = 0.00;
    $obj->fee = 0.00; 
    $obj->cb = 0.00;
    return $obj;
  }
  
  public static function get_t_table($invoice, $finance_date) {
    //TODO
  	$real_start = FinanceUtils::__get_real_start($invoice->start_date_tuition, $finance_date);
  	$real_end = FinanceUtils::__get_real_start($invoice->end_date_tuition, $finance_date);
  	$real_dates = FinanceUtils::__get_real_dates($real_start, $real_end);
  	$total_amount = $invoice->total_tuition;
  	
  	$results = array();
    foreach ($real_dates as $key => $tmp) {
    	if ($invoice->_is_completed == 1) {
	      if ($key == 0) {
	        $obj = new stdClass();
	        $obj->date = $tmp;
	        $obj->ob = round($total_amount, 2);
	        $obj->fee = round(min(500, $total_amount * 0.1), 2);
	        $obj->cb = round($obj->ob - $obj->fee, 2);
	        array_push($results, $obj);
	      } else if ($key == 1) {
	        $obj = new stdClass();
	        $obj->date = $tmp;
	        $obj->ob = round($results[$key-1]->cb, 2);
	        $obj->fee = round($obj->ob * 0.15, 2);
	        $obj->cb = round($obj->ob - $obj->fee, 2);
	        array_push($results, $obj);
	      } else {
	        $leave_amout = $results[1]->cb;
	        $leave_count = count($real_dates) - 2;
	        $every_amount = round($leave_amout / $leave_count, 2);
	        if ($key != count($real_dates) - 1) {
	          $obj = new stdClass();
	          $obj->date = $tmp;
	          $obj->ob =round( $results[$key-1]->cb, 2);
	          $obj->fee = round($every_amount, 2);
	          $obj->cb = round($obj->ob - $obj->fee, 2);
	          array_push($results, $obj);
	        } else {
	          $last_amount = round($leave_amout - (count($real_dates) - 3) * $every_amount, 2);
	          $obj = new stdClass();
	          $obj->date = $tmp;
	          $obj->ob = round($results[$key-1]->cb, 2);
	          $obj->fee = round($last_amount, 2);
	          $obj->cb = round($obj->ob - $obj->fee, 2);
	          array_push($results, $obj);
	        }
	    	}
    	} else {
    		$obj = new stdClass();
        $obj->date = $tmp;
        $obj->ob = round($total_amount, 2);
        $obj->fee = 0.00;
        $obj->cb = round($obj->ob - $obj->fee, 2);
        array_push($results, $obj);
    	}
    }
    return $results;
  }
  
  public static function get_l_table($invoice, $finance_date) {
    //TODO
    $real_start = FinanceUtils::__get_real_start($invoice->start_date_accommodation, $finance_date);
    $real_end = FinanceUtils::__get_real_start($invoice->end_date_accommodation, $finance_date);
    $real_dates = FinanceUtils::__get_real_dates($real_start, $real_end);
    $total_amount = $invoice->total_accommodation;
    $weeks = $invoice->weeks_accommodation;
   
    $every_week_fee = round(0.00, 2);
    if ($weeks > 0) {
      $every_week_fee = round($total_amount / $weeks, 2);
    }
    
    $results = array();
    foreach ($real_dates as $key => $tmp) {
    	if ($invoice->_is_completed == 1) {
	      if ($weeks <= 6) {
	        if ($key == 0) {
	          $obj = new stdClass();
	          $obj->date = $tmp;
	          $obj->ob = round($total_amount, 2);
	          $obj->fee = round($total_amount, 2);
	          $obj->cb = round($obj->ob - $obj->fee, 2);
	          array_push($results, $obj);
	        } else {
	          $obj = new stdClass();
	          $obj->date = $tmp;
	          $obj->ob = round(0.00, 2);
	          $obj->fee = round(0.00, 2);
	          $obj->cb = round($obj->ob - $obj->fee, 2);
	          array_push($results, $obj);
	        }
	      } else {
	        $every_6_amount = round($every_week_fee * 6, 2);
	        $every_2_amount = round($every_week_fee * 2, 2);
	        if ($key == 0) {
	          $obj = new stdClass();
	          $obj->date = $tmp;
	          $obj->ob = round($total_amount, 2);
	          $obj->fee = round($every_6_amount, 2);
	          $obj->cb = round($obj->ob - $obj->fee, 2);
	          array_push($results, $obj);
	        } else {
	          if ($results[$key-1]->cb - $every_2_amount > 0) {
	            $obj = new stdClass();
	            $obj->date = $tmp;
	            $obj->ob = round($results[$key-1]->cb, 2);
	            $obj->fee = round($every_2_amount, 2);
	            $obj->cb = round($obj->ob - $obj->fee, 2);
	            array_push($results, $obj);
	          } else {
	            $obj = new stdClass();
	            $obj->date = $tmp;
	            $obj->ob = round($results[$key-1]->cb, 2);
	            $obj->fee = round($obj->ob, 2);
	            $obj->cb = round($obj->ob - $obj->fee, 2);
	            array_push($results, $obj);
	          }
	        }
        }
    	} else {
        $obj = new stdClass();
        $obj->date = $tmp;
        $obj->ob = round($total_amount, 2);
        $obj->fee = 0.00;
        $obj->cb = round($obj->ob - $obj->fee, 2);
        array_push($results, $obj);
      }
    }
    return $results;
  }
  
  public static function get_i_table($invoice, $finance_date) {
    //TODO
    $real_start = FinanceUtils::__get_real_start($invoice->start_date_insurance, $finance_date);
    $real_end = FinanceUtils::__get_real_start($invoice->end_date_insurance, $finance_date);
    $real_dates = FinanceUtils::__get_real_dates($real_start, $real_end);
    $total_amount = $invoice->total_insurance;
    
    $results = array();
    foreach ($real_dates as $key => $tmp) {
    	if ($invoice->_is_completed == 1) {
	      if ($key == 0) {
	        $obj = new stdClass();
	        $obj->date = $tmp;
	        $obj->ob = round($total_amount, 2);
	        $obj->fee = round($total_amount, 2);
	        $obj->cb = round($obj->ob - $obj->fee, 2);
	        array_push($results, $obj);
	      } else {
	        $obj = new stdClass();
	        $obj->date = $tmp;
	        $obj->ob = round(0.00, 2);
	        $obj->fee = round(0.00, 2);
	        $obj->cb = round($obj->ob - $obj->fee, 2);
	        array_push($results, $obj);
	      }
    	} else {
        $obj = new stdClass();
        $obj->date = $tmp;
        $obj->ob = round($total_amount, 2);
        $obj->fee = 0.00;
        $obj->cb = round($obj->ob - $obj->fee, 2);
        array_push($results, $obj);
      }
    }
    return $results;
  }
  
  public static function __get_real_start($start, $finance_date) {
    $f_stamp = strtotime($finance_date);
    $s_stamp = strtotime($start);
    
    $i = $f_stamp;
    while ($i < $s_stamp) {
      $i += 86400*14;
    }
	  return date('Y-m-d', $i);
  }
  
  public static function __get_real_end($end, $finance_date) {
  	$f_stamp = strtotime($finance_date);
    $e_stamp = strtotime($end);
    
    $i = $f_stamp;
    while ($i < $e_stamp) {
      $i += 86400*14;
    }
    
    return date('Y-m-d', $i);
  }
  
  public static function __get_real_dates($real_start, $real_end) {
    //TODO
    $dates = array();
    $start_stamp = strtotime($real_start);
    $end_stamp = strtotime($real_end);
    for ($i = $start_stamp, $il = $end_stamp; $i <= $il; $i += 86400*14) {
      $dates[] = date('Y-m-d', $i);
    }
    return $dates;
  }
}
?>
